package com.fx.controller;

        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

        import com.alibaba.fastjson.JSONObject;
        import com.fx.entity.OrderInfo;
        import com.fx.entity.WalletInfo;
        import com.fx.service.OrderInfoService;
        import com.fx.service.WalletInfoService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.ui.Model;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.RequestBody;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.ResponseBody;
        import com.fx.entity.UserInfo;
        import com.fx.service.UserInfoService;
        import com.fx.util.*;

        import javax.servlet.http.HttpServletRequest;

/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private WalletInfoService walletInfoService;
    @Autowired
    private OrderInfoService orderInfoService;
    private static String pageTitle = "用户管理";
    private static String pageUrl = "/userInfo";

    @GetMapping()
    String userInfo(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "userInfo/userInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<UserInfo> userInfoList = userInfoService.getList(query);
        int total = userInfoService.getCount(query);
        return renderPage(total, userInfoList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "userInfo/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        UserInfo userInfo =userInfoService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("userInfo", userInfo);
        return "userInfo/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(UserInfo userInfo) {
        String id = userInfo.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = userInfoService.save(userInfo);
        } else {
            result = userInfoService.updateById(userInfo);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (userInfoService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }
    /**
     * 方法名称：个人中心
     * 概要说明：根据用户id获取个人信息
     * @Ather EDZ
     * @Date 2018-6-25 13:57
     * @Param userId 用户Id
     * @Return
     */
    @GetMapping("/api/userCenter")
    @ResponseBody
    public R userCenter(HttpServletRequest request){
        String userId = String.valueOf(request.getAttribute("flId"));
        Map<String,Object> result = new HashMap<String,Object>();
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfo recommend = userInfoService.getById(userInfo.getFlUpUser());
        WalletInfo walletInfo = walletInfoService.getById(userInfo.getFlWallet());
        List<OrderInfo> orderInfoList = orderInfoService.getOrderByUserId(userId);
        result.put("flUpUser",userId);
        List<UserInfo> userInfoList = userInfoService.getUserByLevel(userId,-1);
        result.remove("flUpUser");
        result.put("orderNum",orderInfoList.size());
        result.put("userInfo",userInfo);
        result.put("walletInfo",walletInfo);
        result.put("teamNum",userInfoList.size());
        result.put("recommend",recommend == null ? "系统注册": recommend.getFlName());
        return R.ok(result);
    }

    /**
     * 方法名称：获取指定用户的会员数
     * @Ather EDZ
     * @Date 2018-7-7 9:28
     * @Param [userId 用户Id, level一级:1 二级:2  三级:3 all:-1]
     * @Return com.fx.util.R
     */
    @GetMapping("/api/getMyMember/{level}")
    @ResponseBody
    public R getMyMember(HttpServletRequest request,@PathVariable("level") Integer level){
        String userId = String.valueOf(request.getAttribute("flId"));
        List<UserInfo> userInfoList =  userInfoService.getUserByLevel(userId,level);
        return R.ok(userInfoList);
    }

}
